package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.Answer;
import com.hs.service.AnswerService;
import com.hs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("Answer")
public class AnswerController {
    @Autowired
    private AnswerService answerService;
    @PostMapping
    public R addAnswer(@RequestBody Answer answer) {
        int i = answerService.addAnswer(answer);
        if (i>0){
            return R.ok();
        }else{
            return R.fail("增加失败");
        }
    }
    @DeleteMapping("{id}")
    public R deleteAnswer(@PathVariable int id) {
        int i = answerService.deleteAnswer(id);
        if (i>0){
            return R.ok();
        }else{
            return R.fail("删除失败");
        }
    }
    @PutMapping
    public R updateAnswer(@RequestBody Answer answer) {
        int i = answerService.updateAnswer(answer);
        if (i>0){
            return R.ok();
        }else{
            return R.fail("修改失败");
        }
    }
    @GetMapping("{id}")
    public R getAnswerById(@PathVariable int id) {
        Answer answer = answerService.getAnswerById(id);
        return R.ok(answer);
    }
    @GetMapping("getByPage")
    public R getAnswerByPage(@RequestParam(required = false,defaultValue = "1")int pageNum, @RequestParam(required = false,defaultValue = "5")int pageSize) {
        PageInfo<Answer> answerByPage = answerService.getAnswerByPage(pageNum, pageSize);
        return R.ok(answerByPage);
    }
}
